package com.han.shadow.customerapp.json;

import com.han.shadow.customerapp.bean.Account;
import com.han.shadow.customerapp.bean.Business;
import com.han.shadow.customerapp.bean.Goods;
import com.han.shadow.customerapp.bean.Order;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {

	//JSON到订单list
	public List<Order> Json2ListOrder(String str)throws JSONException{
		JSONArray jsonArray = new JSONArray(str);
		List<Order> list=new ArrayList<Order>();
		Order order=null;
		for(int i = 0; i < jsonArray.length(); i++){
			JSONObject json = (JSONObject)jsonArray.get(i);
			order=new Order();
			if(json.has("oid")) order.setOid(json.getString("oid"));
			if(json.has("bid")) order.setBid(json.getString("bid"));
			if(json.has("cid")) order.setCid(json.getString("cid"));
			if(json.has("note")) order.setNote(json.getString("note"));
			if(json.has("orderTime")) order.setOrderTime(json.getString("orderTime"));
			if(json.has("status")) order.setStatus(json.getInt("status"));
			if(json.has("goodsIDs")){
				Map<String, Integer> goodsIDs = new HashMap<String, Integer>();
				JSONObject jo = json.getJSONObject("goodsIDs");
				Iterator<String> it = jo.keys();
				while(it.hasNext()){
					String key = it.next();
					goodsIDs.put(key,Integer.parseInt(jo.getString(key)));
				}
				order.setGoodsIDs(goodsIDs);
			}

			list.add(order);
		}
		return list;
	}
	//订单list到JSON
	public JSONArray listOrder2Json(List<Order> ls)throws JSONException{
		JSONArray jsonArray = new JSONArray();
		JSONObject jo=null;
		for(Order order:ls){
			jo=new JSONObject();

			JSONObject joMap = new JSONObject();
			jo.put("oid",order.getOid());
			jo.put("bid", order.getBid());
			jo.put("cid",order.getCid());
			for(Map.Entry<String, Integer> entry: order.getGoodsIDs().entrySet()){
				joMap.put(entry.getKey(),entry.getValue());
			}
			jo.put("goodsIDs",joMap);
			jo.put("orderTime",order.getOrderTime());
			jo.put("status",order.getStatus());
			jo.put("note",order.getNote());
			jsonArray.put(jo);
		}
		return jsonArray;
	}

	//商品list到JSON    App:List<Goods>--->json
	public JSONArray listGoods2JSON(List<Goods> ls)throws JSONException{
		JSONArray jsonArray = new JSONArray();
		JSONObject jo=null;
		for(Goods good:ls){
			jo=new JSONObject();
			jo.put("gid",good.getGid());
			jo.put("name", good.getName());
			JSONArray ja = new JSONArray();
			for(String s:good.getPicPath()){
				ja.put(s);
			}
			jo.put("picPath",ja);
			jo.put("bid",good.getBid());
			jo.put("type",good.getType());
			jo.put("price",good.getPrice());
			jo.put("salesVolume",good.getSalesVolume());
			jo.put("evaluation",good.getEvaluation());
			jo.put("selltime", good.getSelltime());
			jo.put("status", good.getStatus());
			jo.put("information", good.getInformation());
			jsonArray.put(jo);
		}
		return jsonArray;
	}

	//JSON到商品   App:json--->ListGoods
	public List<Goods> JSON2listGoods(String str)throws JSONException{
		JSONArray jsonArray = new JSONArray(str);
		List<Goods> list=new ArrayList<Goods>();
		Goods good=null;
		for(int i = 0; i < jsonArray.length(); i++){
			JSONObject json = (JSONObject)jsonArray.get(i);
			good=new Goods();
			if(json.has("bid")) good.setBid(json.getString("bid"));
			if(json.has("gid"))good.setGid(json.getString("gid"));
			if(json.has("name"))good.setName(json.getString("name"));
			if(json.has("picPath")) {
				List<String> picPath = new ArrayList<>();
				JSONArray ja = json.getJSONArray("picPath");
				for (int j = 0; j < ja.length(); j++) {
					picPath.add((String) ja.get(j));
				}
				good.setPicPath(picPath);
			}
			if(json.has("status"))good.setStatus(json.getInt("status"));
			if(json.has("type"))good.setType(json.getInt("type"));
			if(json.has("selltime"))good.setSelltime(json.getString("selltime"));
			if(json.has("evaluation"))good.setEvaluation(json.getString("evaluation"));
			if(json.has("information"))good.setInformation(json.getString("information"));
			if(json.has("salesVolume"))good.setSalesVolume(json.getInt("salesVolume"));
			if(json.has("price"))good.setPrice(json.getInt("price"));
			list.add(good);
		}
		return list;
	}

	//商家list到JSON   Server:ListBusiness--->json
	public JSONArray listBusiness2Json(List<Business> ls)throws JSONException{
		JSONArray jsonArray = new JSONArray();
		JSONObject jo=null;
		JSONObject joAcc = null;
		for(Business bu:ls){
			jo=new JSONObject();
			jo.put("bid",bu.getBid());
			jo.put("name", bu.getName());
			joAcc = new JSONObject();
			joAcc.put("account", bu.getAcc().getAccount());
			joAcc.put("psd", bu.getAcc().getPsd());
			joAcc.put("identity", bu.getAcc().getIdentity());
			jo.put("acc",joAcc);
			jo.put("phone",bu.getPhone());
			jo.put("star",bu.getStar());
			jo.put("evaluation",bu.getEvaluation());
			jo.put("information", bu.getInformation());
			jsonArray.put(jo);
		}
		return jsonArray;
	}
	//JSON到商家list    App:json--->listBusiness
	public List<Business> JSON2listBusiness(String str)throws JSONException {
		JSONArray jsonArray = new JSONArray(str);
		List<Business> list=new ArrayList<Business>();
		Business bu=null;
		for(int i = 0; i < jsonArray.length(); i++){
			JSONObject json = (JSONObject)jsonArray.get(i);
			bu=new Business();
			if(json.has("bid")) bu.setBid(json.getString("bid"));
			if(json.has("evaluation"))bu.setEvaluation(json.getString("evaluation"));
			if(json.has("information"))bu.setInformation(json.getString("information"));
			if(json.has("name"))bu.setName(json.getString("name"));
			if(json.has("phone"))bu.setPhone(json.getString("phone"));
			if(json.has("star"))	bu.setStar(json.getInt("star"));

			if(json.has("acc")) {
				JSONObject j = json.getJSONObject("acc");
				Account acc = new Account();
				if (json.has("psd")) acc.setPsd(j.getString("psd"));
				if (json.has("identity")) acc.setIdentity(j.getInt("identity"));
				if (json.has("account")) acc.setAccount(j.getString("account"));
				bu.setAcc(acc);
			}
			list.add(bu);
		}
		return list;
	}
	
}
